package org.testmyapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.testmyapp.entities.Access;
import org.testmyapp.persistence.BusinessObjectPersistenceProvider;

public class MyServlet extends HttpServlet implements Servlet {

	@Inject
	private BusinessObjectPersistenceProvider bopProvider;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("blub blub blub");
		super.doPost(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		PrintWriter pw = resp.getWriter();
		pw.println("<h1>");
		Date date = new Date();
		pw.println("magic happenin on " + date);
		pw.println("</h1>");
		ProcessEngines.init();

		ProcessEngineConfiguration engConf = ProcessEngineConfiguration
				.createStandaloneProcessEngineConfiguration();
		engConf.setDataSourceJndiName("jdbc/MyPostgres");
		engConf.setHistory("full");
		ProcessEngine buildProcessEngine = engConf.buildProcessEngine();
		if (buildProcessEngine != null) {
			pw.println("<br>Got my process engine ready!");
		} else {
			pw.println("<br>Couldn't process engine ready!");
		}
		Deployment deploy = buildProcessEngine.getRepositoryService()
				.createDeployment()
				.addClasspathResource("diagrams/BelegKorrektur.bpmn20.xml")
				.addClasspathResource("diagrams/BelegeErfassen.bpmn20.xml")
				.addClasspathResource("diagrams/Erstattungsberechtigung.bpmn20.xml")
				.addClasspathResource("diagrams/Erstattungshoehe.bpmn20.xml")
				.addClasspathResource("diagrams/Leistungsabrechnung.bpmn20.xml")
				.addClasspathResource("diagrams/ManuelleKorrektur.bpmn20.xml")
				.addClasspathResource("diagrams/TimerTesting.bpmn20.xml")
				.addClasspathResource("diagrams/MoreUserTasks.bpmn20.xml")
				.addClasspathResource("diagrams/DemoUserUI.bpmn20.xml")
				.addClasspathResource("diagrams/TestEvents.bpmn20.xml")
				.addClasspathResource("diagrams/PerformanceProcess.bpmn20.xml")
				.deploy();
		if (deploy != null) {
			pw.println("<br>deployed: " + deploy.getId() + " at "
					+ deploy.getDeploymentTime());
			List<ProcessDefinition> processes = buildProcessEngine.getRepositoryService()
					.createProcessDefinitionQuery()
					.deploymentId(deploy.getId()).list();
			for(ProcessDefinition pDef : processes) {
				pw.println("<br>Deployt process " + pDef.getName() + "(" + pDef.getId() + ")");
			}
			
		} else {
			pw.println("<br>Couldn't deploy my shit!");
		}
	}
}
